import sqlite3
import os

"""
Raises LoginError if login fails
Returns sid if login successful
"""
def add_picture(album_id, user_id, filename, label):
  # Insert picture
  conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
      + "/PictureShare/pygallery.sqlite")
  session_cur = conn.cursor()
  session_cur.execute("""
    insert into pictures (picture_id, album_id, user_id, file_name, label) 
    values (NULL, ?, ?, ?, ?)
    """, (album_id, user_id, filename, label))
  session_cur.close()
  conn.commit()
  conn.close()

# prefer username instead of user id because we would end up
# just using the user id to look up the username for dir to 
# delete from
def remove_picture(album_id, username, pic_id):
  picture = get_filename_by_id(pic_id)
  if picture is None:
    return
  conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
      + "/PictureShare/pygallery.sqlite")
  session_cur = conn.cursor()
  session_cur.execute("delete from pictures where picture_id=?", [pic_id])
  session_cur.execute("""
    insert into pictures_deleted 
    (picture_id, album_id, user_id, file_name, label)
    values (?, ?, ?, ?, ?)
    """, (picture["picture_id"], picture["album_id"], picture["user_id"],
    picture["file_name"], picture["label"]))
  session_cur.close()
  conn.commit()
  conn.close()
  # Check if there are two files that refer to the same file
  """
  conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
      + "/PictureShare/pygallery.sqlite")
  session_cur = conn.cursor()
  session_cur.execute("select * from pictures where file_name=?", 
      [picture["file_name"]])
  exists = session_cur.fetchone()
  session_cur.close()
  conn.close()
  if exists is None: # No other references to this file
    # Actually delete from dir
    os.remove(os.environ["DOCUMENT_ROOT"]
          + "/PictureShare/users/"
          + username + "/"
          + "album"+ str(album_id) + "/"
          + picture["file_name"])
    os.remove(os.environ["DOCUMENT_ROOT"]
          + "/PictureShare/users/"
          + username + "/"
          + "albumthumb"+ str(album_id) + "/"
          + picture["file_name"])
  """

def reactivate_picture(album_id, username, pic_id):
  picture = get_deleted_picture(pic_id)
  if picture is None:
    return
  conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
      + "/PictureShare/pygallery.sqlite")
  session_cur = conn.cursor()
  session_cur.execute("delete from pictures_deleted where picture_id=?", 
      [pic_id])
  session_cur.execute("""
    insert into pictures 
    (picture_id, album_id, user_id, file_name, label)
    values (?, ?, ?, ?, ?)
    """, (picture["picture_id"], picture["album_id"], picture["user_id"],
    picture["file_name"], picture["label"]))
  session_cur.close()
  conn.commit()
  conn.close()

# picid: int , value to be looked up      
def get_deleted_picture(picid):
  conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
      + "/PictureShare/pygallery.sqlite")
  conn.row_factory = sqlite3.Row
  picture_cur = conn.cursor()
  picture_cur.execute(
      "select p.* from pictures_deleted p where p.picture_id = ?",
      (picid,))
  picture = picture_cur.fetchone()
  picture_cur.close()
  conn.close()
  return picture       

# Takes an id and returns the filename associated with it
# picid: int , value to be looked up      
def get_filename_by_id(picid):
  conn = sqlite3.connect(os.environ["DOCUMENT_ROOT"] 
      + "/PictureShare/pygallery.sqlite")
  conn.row_factory = sqlite3.Row
  picture_cur = conn.cursor()
  picture_cur.execute("select p.* from pictures p where p.picture_id = ?", \
      (picid,))
  picture = picture_cur.fetchone()
  picture_cur.close()
  conn.close()
  return picture
    
